package com.bt.service.impl;

import com.bt.constant.MessageConstant;
import com.bt.dto.YUserLoginDTO;
import com.bt.dto.YddUserNewDTO;
import com.bt.entity.*;
import com.bt.exception.AccountNotFoundException;
import com.bt.exception.PasswordErrorException;
import com.bt.exception.UserExistException;
import com.bt.mapper.YddUserIndexMapper;
import com.bt.mapper.YddUserMapper;
import com.bt.mapper.*;
import com.bt.result.PageResult;
import com.bt.service.YddUserIndexService;
import com.bt.utils.MD5Utils;
import com.bt.vo.ShowVO;
import com.bt.vo.UserIndexShowVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Description 前台基础数据实现类
 * @Author 张传康
 * @Date 2023-05-04 22:53
 */
@Slf4j
@Service
public class YddUserIndexServiceImpl implements YddUserIndexService {
    @Autowired
    private YddUserMapper yddUserMapper;
    @Autowired
    private YddUserIndexMapper yddUserIndexMapper;
    @Autowired
    private YddLinkMapper yddLinkMapper;
    @Autowired
    private YddCateMapper yddCateMapper;
    @Autowired
    private YddTagMapper yddTagMapper;
    @Autowired
    private YddAdvimgMapper yddAdvimgMapper;

    /**
     * 注册用户
     *
     * @param yUserLoginDTO
     * @return
     */
    @Override
    public YddUser req(YUserLoginDTO yUserLoginDTO) {
        // 判断是否存在这个用户，如果存在，就返回用户已存在
        YddUser byMobile = yddUserMapper.findByMobile(yUserLoginDTO.getMobile());
        if (byMobile!=null){
            throw new UserExistException(MessageConstant.ACCOUNT_EXIST);
        }
        String code = MD5Utils.code(yUserLoginDTO.getPassword());
        YddUser yddUser = new YddUser();
        BeanUtils.copyProperties(yUserLoginDTO, yddUser);
        yddUser.setPassword(code);
        yddUser.setUsername(yUserLoginDTO.getMobile());
        yddUser.setSex(1);
        // 如果用户不存在，就创建用户,并返回用户数据
        yddUserMapper.reg(yddUser);

        return yddUserMapper.findByMobile(yUserLoginDTO.getMobile());
    }

    /**
     * 用户登录
     * 张传康
     * @param yUserLoginDTO
     * @return
     */
    @Override
    public YddUser login(YUserLoginDTO yUserLoginDTO) {

        // 从数据模型中获取用户的手机号
        String mobile = yUserLoginDTO.getMobile();
        // 从数据模型中获取用户密码
        String password = yUserLoginDTO.getPassword();

        // 根据手机号查询用户信息
        YddUser yddUser = yddUserMapper.findByMobile(mobile);

        // 判断是否存在这个用户
        if (yddUser == null){
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        // 比对密码
        String code = MD5Utils.code(password);
        log.info("密码是：{}",code);
        if (!code.equals(yddUser.getPassword())){
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        return yddUser;
    }

    /**
     * 热门置顶文章数据(首页) 的 分页数据
     *
     * @param page
     * @param limit
     * @return
     */
    @Override
    public PageResult page(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);

        // 执行查询条件 2表查询 ydd_article  ydd_cate
        Page<YddUserNewDTO> pageInfo = yddUserIndexMapper.pageNew();

        return new PageResult(pageInfo.getTotal(), pageInfo.getPages(),
                pageInfo.getPageSize(), pageInfo.getPageNum(), pageInfo.getResult());
    }

    /**
     * 24小时最新文章数量
     *
     * @return
     */
    @Override
    public Integer numDay() {
        return 12;
    }

    /**
     * 一周文章最新数量
     *
     * @return
     */
    @Override
    public Integer numWeek() {
        return 12;
    }


    /**
     * 文章详情数据
     *
     * @param id
     * @return
     */
    @Override
    public ShowVO show(Integer id) {
        ShowVO showVO = new ShowVO();
        //查询本篇
        YddArticle article = yddUserIndexMapper.show(id);
        UserIndexShowVO userIndexShowVO = new UserIndexShowVO();
        BeanUtils.copyProperties(article,userIndexShowVO);
        //补全数据
        userIndexShowVO.setDefault_data(null);
        userIndexShowVO.setIsLike(null);
        userIndexShowVO.setIsCollect(null);
        showVO.setInfo(userIndexShowVO);
        //查询上一篇
        List<UserPrev> prevList= yddUserIndexMapper.findPrev(id - 1);
        //判空
        if (Objects.nonNull(prevList)) {
            showVO.setPrev(prevList);
        }
        //查询下一篇
        List<UserNext> userNexts  = yddUserIndexMapper.findNext(id + 1);
        //判空
        if (userNexts.size()>0) {
        showVO.setNext(userNexts);
        }
        //查询推荐文章数据
        List<UserRecommend> userRecommends = yddUserIndexMapper.findRecommend(id);
        showVO.setRecommend(userRecommends);

        return showVO;
    }

    /**
     * 文章列表数据（根据分类id（cateid)展示文章
     *
     * @param page
     * @param limit
     * @param cateid
     * @return
     */
    @Override
    public PageResult pageList(Integer page, Integer limit, Integer cateid) {
        PageHelper.startPage(page,limit);

        // 执行查询条件cateid      2表查询 ydd_article  ydd_cate
        Page<YddUserNewDTO> pageInfo = yddUserIndexMapper.pageList(cateid);

        return new PageResult(pageInfo.getTotal(), pageInfo.getPages(),
                pageInfo.getPageSize(), pageInfo.getPageNum(), pageInfo.getResult());
    }

    /**
     * 查询所有分类
     *
     * @return
     */
    @Override
    public List<YddCate> findAllCate() {
        List<YddCate> all = yddCateMapper.getAll();
        return all;
    }

    /**
     * 查询所有友情链接
     *
     * @return
     */
    @Override
    public List<YddLink> findAllLink() {
        List<YddLink> findAllLink = yddLinkMapper.findAll();
        return findAllLink;
    }

    /**
     * 查询所有标签
     *
     * @return
     */
    @Override
    public List<YddTag> findAllTag() {
        List<YddTag> findAllTag = yddTagMapper.all();
        return findAllTag;
    }

    /**
     * 轮播图数据  advimgpos=15
     *
     * @param advimgpos
     * @return
     */
    @Override
    public List<YddAdvimg> findBanner(Integer advimgpos) {
        List<YddAdvimg> findBanner= yddAdvimgMapper.findBanner(advimgpos);
        return findBanner;
    }

    /**
     * 详情广告图 advimgpos=13 or advimgpos=15
     *
     * @param advimgpos1
     * @param advimgpos2
     * @return
     */
    @Override
    public List<YddAdvimg> findShowBanner(Integer advimgpos1, Integer advimgpos2) {
        List<YddAdvimg> showBanner = yddAdvimgMapper.findBanner2(advimgpos1, advimgpos2);
        return showBanner;
    }

    /**
     * 侧边栏图片 advimgpos=14
     *
     * @param advimgpos
     * @return
     */
    @Override
    public List<YddAdvimg> findSideBanner(Integer advimgpos) {
        List<YddAdvimg> sideBanner = yddAdvimgMapper.findBanner(advimgpos);
        return sideBanner;
    }

    /**
     * 最新文章
     *
     * @return
     */
    @Override
    public List<YddUserNewDTO> findHot() {
        Page<YddUserNewDTO> yddUserNewDTOS = yddUserIndexMapper.hot();
        return yddUserNewDTOS;
    }

    /**
     * 推荐文章
     *
     * @return
     */
    @Override
    public List<YddUserNewDTO> findRecommend() {
        Page<YddUserNewDTO> yddUserNewDTOS = yddUserIndexMapper.recommend();

        return yddUserNewDTOS;
    }
}
